package com.maxd.controller;

import com.google.common.collect.Lists;
import com.maxd.bean.PageRes;
import com.maxd.error.Result;
import com.maxd.error.ResultUtil;
import com.maxd.model.StudentAppraisalRec;
import com.maxd.service.inf.IStudentAppraisalService;
import com.maxd.utils.AdminUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.persistence.criteria.Predicate;
import java.util.List;

@RestController
@Api(value="学生综合测评管理", tags = {"学生综合测评管理"})
@RequestMapping(value = "/student-appraisal")
public class StudentAppraisalController {

    @Autowired
    private IStudentAppraisalService studentAppraisalService;


    @ApiOperation("新增/修改学生综合测评信息")
    @PostMapping("/save")
    public Result<Void> save(@RequestBody @Validated StudentAppraisalRec studentAppraisalRec) {
        studentAppraisalService.save(studentAppraisalRec);
        return ResultUtil.success();
    }

    @ApiOperation("内管分页查询学生综合测评列表")
    @GetMapping("/appraisal-page")
    public Result<PageRes<StudentAppraisalRec>> queryPage(@ApiParam("页码，从0页开始") @RequestParam("pageNo") Integer pageNo,
                                                       @ApiParam("每页记录数量") @RequestParam("pageSize") Integer pageSize,
                                                       @ApiParam("学员ID") @RequestParam(value = "studentId", required = false) Long studentId) {
        AdminUtils.checkLogin();
        // 分页参数
        PageRes<StudentAppraisalRec> pageRes = studentAppraisalService.queryPage(pageNo, pageSize, studentId);
        return ResultUtil.success(pageRes);
    }

    @ApiOperation("商城查询学生综合测评列表")
    @GetMapping("/student-appraisal-list")
    public Result<List<StudentAppraisalRec>> myList(@ApiParam("学员ID") @RequestParam("studentId") Long studentId) {
        // 分页参数
        List<StudentAppraisalRec> recList = studentAppraisalService.queryList(studentId);
        return ResultUtil.success(recList);
    }

    private Specification<StudentAppraisalRec> buildCondition(Long studentId) {
        Specification<StudentAppraisalRec> queryCondition = (root, query, criteriaBuilder) -> {
            List<Predicate> predicates = Lists.newArrayList();
            if(studentId != null) {
                predicates.add(criteriaBuilder.equal(root.get("studentId"), studentId));
            }
            return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
        };
        return queryCondition;
    }

}
